package db.seed;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

import static com.mai4j.maiadmin.generated.Tables.ADMIN_MENU;
import static com.mai4j.maiadmin.generated.Tables.ADMIN_MENU_DESCRIPTION;
import static org.jooq.impl.DSL.using;


/**
 * 初始化默认菜单
 */
@Slf4j
public class V20230404162156__Insert_AdminMenu extends BaseJavaMigration {

    static final Long DEFAULT_ADMIN_MENU_ID = 300314610275568654L;

    @SneakyThrows
    @Override
    public void migrate(Context context) {
        using(context.getConnection())
                .insertInto(ADMIN_MENU)
                .columns(ADMIN_MENU.ADMIN_MENU_ID,
                        ADMIN_MENU.CONTENT,
                        ADMIN_MENU.STATUS,
                        ADMIN_MENU.DATE_ADDED,
                        ADMIN_MENU.DATE_MODIFIED
                )
                .values(DEFAULT_ADMIN_MENU_ID,
                        IOUtils.resourceToString("/db/admin-menus.json", StandardCharsets.UTF_8),
                        (byte) 1,
                        LocalDateTime.now(),
                        LocalDateTime.now())
                .execute();
        using(context.getConnection())
                .insertInto(ADMIN_MENU_DESCRIPTION)
                .columns(
                        ADMIN_MENU_DESCRIPTION.ADMIN_MENU_ID,
                        ADMIN_MENU_DESCRIPTION.LANGUAGE_CODE,
                        ADMIN_MENU_DESCRIPTION.NAME
                )
                .values(DEFAULT_ADMIN_MENU_ID,
                        "zh-CN",
                        "默认菜单")
                .values(DEFAULT_ADMIN_MENU_ID,
                        "en-US",
                        "Default menu")
                .execute();
    }
}